<template>
  <view>
    <button @click="login">微信登录</button>
    <button @click="subscribe">用户是否关注（登陆后执行）</button>
    <button @click="share">设置微信分享样式</button>
    <button @click="notice">发送微信模板消息</button>
    <button @click="scanQRCode">微信扫一扫</button>
    <button @click="hideMenuItems">隐藏菜单分享按钮</button>
  </view>
</template>

<script>
// #ifdef H5
import wxH5 from '@/utils/wx-public/wxH5.js'
// #endif

export default {
  data() {
    return {

    }
  },
  async onLoad() {
    // await this.share()
  },
  async onShow() {
    // #ifdef H5
    if (wxH5.isWechat()) {
      let showloading = true // 显示登陆中
      let res = await wxH5.wxH5LoginSep2(showloading)
      /* 获取用户openid和用户信息 */
      console.log(res)
      // 自定义登录注册方法...
    }
    // #endif
  },
  methods: {
    async hideMenuItems() {
      if (wxH5.isWechat()) {
        let menuList = ['menuItem:share:appMessage', 'menuItem:share:timeline']
        let res = await wxH5.hideMenuItems(menuList)
        console.log(res)
      } else {
        uni.showModal({
          content: '不在微信客户端'
        })
      }
    },
    async scanQRCode() {
      if (wxH5.isWechat()) {
        let res = await wxH5.scanQRCode()
        console.log(res)
      } else {
        uni.showModal({
          content: '不在微信客户端'
        })
      }
    },
    async login() {
      let res = await wxH5.wxH5LoginSep1()
      console.log(res)
    },
    async share() {
      if (wxH5.isWechat()) {
        let imgurl = ''// 图片链接
        let title = '标题'
        let desc = '副标题'
        let url = window.location.href
        await wxH5.shareSet(title, desc, imgurl, url)
      } else {
        uni.showModal({
          content: '不在微信客户端'
        })
      }
    },
    async notice() {
      // #ifdef H5
      let url = window.location.href.split('#')[0]
      let noticedata = {}
      let template_id = 'CjyawW7siCW6MCy-bJ3wtcVss5ifYY_YaPJc3Kr-8z0'
      let allopenid = ['odC556Cq2f7FR-_2EJZhA0XhG83Q']
      let res = await wxH5.notice(allopenid, url, noticedata, template_id)
      console.log(res)
      if (res.result.success) {
        uni.showLoading({
          title: '成功通知人数：' + res.result.num,
          icon: 'success',
          duration: 2000
        })
        setTimeout(() => {
          uni.hideLoading()
        }, 2000)
      }

      // #endif
    },
    async subscribe() {
      let openid = uni.getStorageSync('openid')
      if (openid) {
        // #ifdef H5
        let res = await wxH5.subscribe(openid)
        console.log(res)
        // #endif
      } else {
        uni.showModal({
          content: '请先登录'
        })
      }
    }
  }
}
</script>

<style></style>
